package com.stu.work.servlet;

import com.stu.work.mapper.IAdminMapper;
import com.stu.work.vo.AdminAndBookAndNoticeVo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.session.TransactionIsolationLevel;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;

/**
 * ClassName: AdminServlet
 * Package: com.stu.work.servlet
 * Description: @Version 1.0
 */
@WebServlet("/admin")
public class AdminServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter printWriter=response.getWriter();

        SqlSession session=null;
        String resource = "mybatis-config.xml";
        // 读取总配置文件
        InputStream inputStream = Resources.getResourceAsStream(resource);
        // 获取session工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        // 获取session
        session = sqlSessionFactory.openSession();

        List<AdminAndBookAndNoticeVo> adminAndBookAndNotice = session.getMapper(IAdminMapper.class).getAdminAndBookAndNotice();
//        printWriter.println(adminAndBookAndNotice);

        for (AdminAndBookAndNoticeVo eve:adminAndBookAndNotice ) {
            printWriter.println(eve+"<br>");
        }

        session.commit();
    }
}
